<?php

add_action('wp_head', 'head_buffer');
/**
* Start our buffer and Use filters as actions and add them to end our buffer depending on the header image
*
*/
function head_buffer(){
	ob_start();

	if ( is_singular() && has_post_thumbnail( $post->ID ) && $width >= HEADER_IMAGE_WIDTH ) :
		add_filter('post_thumbnail_html', 'clear_buffer_post_thumbnail');
	else :
		 add_filter('theme_mod_header_image', 'clear_buffer_header_image');
	endif;


}


/**
* Our filter is really an action
*
*/
function clear_buffer_header_image($html){
	clear_buffer();
	return $html;
}

/**
* This adds the content of our widget.  Could we use a new sidebar-*.php file?  yes, but it's easier to keep all of this in one file
*
*/

function new_header_widget(){


	ob_start();
?>
		<div id="headerwidget" class="widget-area">
			<ul class="xoxo">
<?php if ( ! dynamic_sidebar( 'header-widget-area' ) ) : // begin Header widget area ?>
			<li id="search" class="widget-container widget_search">
				<?php get_search_form(); ?>
			</li>

			<li id="archives" class="widget-container">
				 <h3 class="widget-title"><?php _e( 'The Maker', 'dirtyten' ); ?></h3>

                <ul>

                    <li><a href='http://aaron.jorb.in'>Wordpress Developer Aaron Jorbin</a> creates themes and plugins for both the masses and the individual</li>

                    <li><a href='http://aaron.jorb.in/thirtyten'>Thirty Ten</a> is a Twenty Ten Child theme designed to teach you how to make child themes</li>

                </ul>

			</li>

			<li id="meta" class="widget-container">
				<h3 class="widget-title"><?php _e( 'Meta', 'twentyten' ); ?></h3>
				<ul>
					<li><?php echo "<a href='".get_bloginfo('rss2_url')."' >Subscribe</a>";  ?></li>
					<?php wp_register(); ?>
					<li><?php wp_loginout(); ?></li>
					<?php wp_meta(); ?>

				</ul>
			</li>
<?php endif; // end Header widget area ?>
			</ul>
		</div><!-- #headerwidget .widget-area -->
<?
	$widget_area = ob_get_clean();
	return $widget_area;
}

/**
* The Buffer ender. Clear it, add to it, echo it.
*
* Replace the blank space after our site description div with our header widget.  This is called by both
* of our action psuedo filters
*/
function clear_buffer(){
	$buffer = ob_get_clean();
	$header = 	preg_replace('#<div id="site-description">.*</div>#',"$0".new_header_widget() ,$buffer);

	//var_dump ($header);

	echo $header;
}

function clear_buffer_post_thumbnail($html){
	clear_buffer();
	return $html;
}


add_action( 'init', 'dirtyten_widgets_init' );

/**
* Register our widget
*
*/
function dirtyten_widgets_init() {
	// Area 1
	register_sidebar( array (
		'name' => 'Header Widget Area',
		'id' => 'header-widget-area',
		'description' => __( 'The widget area in the Header.  Optimized to use three widgets' , 'dirtyten' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => "</li>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );


}

?>
